/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.kimstore.main.controller;

import com.kimstore.bean.Customer;
import com.kimstore.bean.Order;
import com.kimstore.bean.Staff;
import com.kimstore.factory.DaoFactory;
import com.kimstore.gui.main.Inventory;
import com.kimstore.gui.staff.NewStaffController;
import com.kimstore.gui.staff.ViewStaffController;
import com.kimstore.utilities.CustomTableRowSorter;
import com.kimstore.utilities.DateUtil;
import com.kimstore.utilities.PaginateSearchAndFilter;
import java.awt.MouseInfo;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import javax.swing.table.DefaultTableModel;
import org.jdesktop.swingx.decorator.HighlighterFactory;

/**
 *
 * @author Administrator
 */
public class StaffTabController extends BaseTabController {

    private Inventory parentController;
    private PaginateSearchAndFilter<Staff> psaf;

    public StaffTabController(Inventory parentController) {
        this.parentController = parentController;
    }

    public void populateStaffTable() {
        populateStaffTable(psaf.search());
    }

    private void populateStaffTable(List<Staff> staffs) {
        DefaultTableModel model = (DefaultTableModel) table.getModel();
        model.setRowCount(0);
        for (Staff s : staffs) {
            List<Order> orders = DaoFactory.getInstance().getOrderDao().findByStaff(s);

            model.insertRow(model.getRowCount(), new Object[]{s.getId(), s.getName(), (orders != null && orders.size() > 0) ? DateUtil.formatDate(orders.get(orders.size() - 1).getOrderDate()) : "None"});
        }
    }

    public void clearTable() {
        DefaultTableModel model = (DefaultTableModel) table.getModel();
        model.setRowCount(0);
    }

    public void refreshTable() {
        clearTable();
        populateStaffTable();
    }

    public void createNewStaffController() {
        NewStaffController controller = new NewStaffController(this);
        controller.displayWindow();
    }

    public void createNewStaffControllerCallback(Staff staff) {
        refreshTable();
    }

    public void createViewStaffController(String staffId) {
        ViewStaffController controller = new ViewStaffController(this, Integer.parseInt(staffId));
        controller.displayWindow();
    }

    public void createViewStaffControllerCallback() {
    }

    @Override
    public void prepareUserInterface() {

        table.setShowGrid(true, true);
        table.addHighlighter(HighlighterFactory.createComputedForegroundHighlighter());
    }

    @Override
    public void bindElements() {
        table.setRowSorter(new CustomTableRowSorter(table.getRowSorter().getModel()) {
            @Override
            public void sort() {
                if (table.isShowing()) {
                    Point p = MouseInfo.getPointerInfo().getLocation();
                    int column = getSelectedColumn(p);
                    if (psaf.isColumnSortable(column)) {
                        populateStaffTable(psaf.sort(column));
                    }
                }
            }
        });
        searchButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                populateStaffTable(psaf.search());
            }
        });
        nextButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                List<Staff> staffs = psaf.nextPage();
                if (staffs != null) {
                    populateStaffTable(staffs);
                }
            }
        });

        prevButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                List<Staff> staffs = psaf.previousPage();
                if (staffs != null) {
                    populateStaffTable(staffs);
                }
            }
        });
    }

    private int getSelectedColumn(Point point) {
        int mousex = point.x - table.getLocationOnScreen().x;
        int mousey = point.y - table.getLocationOnScreen().y;
        Point mousep = new Point(mousex, mousey);

        return table.columnAtPoint(mousep);
    }

    public PaginateSearchAndFilter<Staff> getPsaf() {
        return psaf;
    }

    public void setPsaf(PaginateSearchAndFilter<Staff> psaf) {
        this.psaf = psaf;
    }
}
